package com.ruoyi.quartz.service.impl;

import com.ruoyi.common.exception.job.TaskException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.quartz.domain.IotJob;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.mapper.IotJobMapper;
import com.ruoyi.quartz.service.IIotJobService;
import com.ruoyi.quartz.service.ISysJobService;
import jakarta.annotation.Resource;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 定时任务Service业务层处理
 * 
 * @author changkang
 * @date 2025-05-01
 */
@Service
public class IotJobServiceImpl implements IIotJobService {

    @Resource
    private IotJobMapper iotJobMapper;

    @Resource
    private ISysJobService jobService;

    /**
     * 查询定时任务
     * 
     * @param id 定时任务主键
     * @return 定时任务
     */
    @Override
    public IotJob selectIotJobById(Long id)
    {
        return iotJobMapper.selectIotJobById(id);
    }

    /**
     * 查询定时任务列表
     * 
     * @param iotJob 定时任务
     * @return 定时任务
     */
    @Override
    public List<IotJob> selectIotJobList(IotJob iotJob)
    {
        return iotJobMapper.selectIotJobList(iotJob);
    }

    /**
     * 新增定时任务
     * 
     * @param iotJob 定时任务
     * @return 结果
     */
    @Transactional
    @Override
    public int insertIotJob(IotJob iotJob) throws SchedulerException, TaskException {
        iotJob.setCreateTime(DateUtils.getNowDate());
        SysJob sysJob = iotJob.ToSysJob();
        jobService.insertJob(sysJob);
        iotJob.setSysJobId(sysJob.getJobId());
        return iotJobMapper.insertIotJob(iotJob);
    }

    /**
     * 修改定时任务
     * 
     * @param iotJob 定时任务
     * @return 结果
     */
    @Transactional
    @Override
    public int updateIotJob(IotJob iotJob) throws SchedulerException, TaskException {
        IotJob newIotJob  = iotJobMapper.selectIotJobById(iotJob.getId());
        SysJob newJob = jobService.selectJobById(newIotJob.getSysJobId());
        newJob.setJobName(iotJob.getName());
        newJob.setInvokeTarget(iotJob.getTarget());
        jobService.updateJob(newJob);
        return iotJobMapper.updateIotJob(iotJob);
    }

    @Transactional
    @Override
    public int changeStatus(IotJob iotJob) throws SchedulerException, TaskException {
        IotJob newIotJob  = iotJobMapper.selectIotJobById(iotJob.getId());
        SysJob newJob = jobService.selectJobById(newIotJob.getSysJobId());
        newJob.setStatus(iotJob.getStatus());
        jobService.changeStatus(newJob);
        newIotJob.setStatus(iotJob.getStatus());
        iotJobMapper.updateIotJob(newIotJob);
        return 0;
    }

    @Override
    public boolean run(IotJob iotJob) throws SchedulerException {
        IotJob newIotJob  = iotJobMapper.selectIotJobById(iotJob.getId());
        SysJob newJob = jobService.selectJobById(newIotJob.getSysJobId());
        return jobService.run(newJob);
    }

    /**
     * 批量删除定时任务
     * 
     * @param ids 需要删除的定时任务主键
     * @return 结果
     */
    @Override
    public int deleteIotJobByIds(Long[] ids) throws SchedulerException {
        for (Long id : ids) {
            deleteIotJobById(id);
        }
        return 0;
    }

    /**
     * 删除定时任务信息
     * 
     * @param id 定时任务主键
     * @return 结果
     */
    @Override
    public int deleteIotJobById(Long id) throws SchedulerException {
        IotJob iotJob  = iotJobMapper.selectIotJobById(id);
        jobService.deleteJobByIds(new Long[]{iotJob.getSysJobId()});
        return iotJobMapper.deleteIotJobById(id);
    }
}
